function SceneEditor (scene){
	this.scene = scene;
	this.offsetLeft = 0;
	this.offsetRight = 0;
	this.offsetTop = 0;
	this.offsetBottom = 0;
	this.cellSize = 0;
	
	
	this.cellWidth = 0;
	this.cellHeight = 0;
	
	this.realCellWidth = 0;
	this.realCellHeight = 0;
	
	this.currentShape = null;
	
	this.painter = null;
	
	this.totalRows = 0;
	this.totalColumns = 0;
	
	this.updateData = function(){
		if(this.cellSize<=0) return;
		if(this.scene){
			var sceneSize = this.scene.getSize();
			this.totalRows = 100 - this.offsetTop - this.offsetBottom;
			
			var sceneSize = this.scene.getSize();
			var availableWidth = 100-this.offsetLeft-this.offsetRight;
			var availableHeight = 100-this.offsetTop-this.offsetBottom;
			
			this.cellWidth = this.cellSize;
			this.cellHeight = this.cellSize * sceneSize.w/sceneSize.h;
			
			this.realCellWidth = this.cellWidth * sceneSize.w/100;
			this.realCellHeight = this.cellHeight * sceneSize.h/100;
			
			
			this.totalRows = Math.floor(availableHeight/this.cellHeight);
			this.totalColumns = Math.floor(availableWidth/this.cellWidth);
		}
	}
	
	this.init = function(){
		var self = this;
		var getRealPos = function(pos){
			var sceneSize = self.scene.getSize();
			var col = Math.round((pos.x * 100 / sceneSize.w-self.offsetLeft) / self.cellWidth);
			var row = Math.round((pos.y * 100 / sceneSize.h-self.offsetTop) / self.cellHeight);
			return {x:self.offsetLeft + col * self.cellWidth,y:self.offsetTop + row * self.cellHeight};
		}
		
		// overwrite events
		this.scene.mouseDown = function(pos){
			if(self.painter){
				self.painter.createShapeCheckpoint(getRealPos(pos));
			}

		};	
		this.scene.mouseMove = function(pos){				
			if(self.painter){
				self.painter.createShapeProgress(getRealPos(pos));
			}
		};
	};
	this.startResponsiveIrregularShape = function(){
		this.painter = new ResponsiveIrregularShapePainter(this.scene);
	}
	
	this.draw = function(){
		this.init();
		this.scene.clear();
		this.drawGrid();
		this.scene.invalidate();		
	}
	this.drawGrid = function(){
		
		if(this.cellSize<=0) return;
		var sceneSize = this.scene.getSize();
		var availableWidth = 100-this.offsetLeft-this.offsetRight;
		var availableHeight = 100-this.offsetTop-this.offsetBottom;
		
		this.cellWidth = this.cellSize;
		this.cellHeight = this.cellSize * sceneSize.w/sceneSize.h;
		
		
		var rows = Math.floor(availableHeight/this.cellHeight);
		var cols = Math.floor(availableWidth/this.cellWidth);
		// draw grid vertical lines
		for(var i=0;i<cols+1;i++){
			var line = new ResponsiveLine(this.offsetLeft + i * this.cellWidth , this.offsetTop , 0, rows * this.cellHeight);
			this.scene.add(line);
		}
		// draw grid horiz lines
		for(var i=0;i<rows+1;i++){
			
			var line = new ResponsiveLine(this.offsetLeft, this.offsetTop+i*this.cellHeight, cols*this.cellWidth, 0);
			this.scene.add(line);
		}
	}
		
	this.painters = new Array();
	this.finalizeShape = function(){
		if(this.painter){
			this.painter.finalize();
			this.painters.push(this.painter);
			this.painter = null;
		}		
	}	

	this.serialize = function(){		
		var str = "[";
		var first = true;
		for(var i=0;i<this.scene.shapes.length;i++){
			var shape = this.scene.shapes[i];
			if(shape.painter){
				// shape was painted
				if(!first) str +=",";
				else first = false;
				str += shape.painter.serialize();
			}
		}
		str+="];"
		return str;
	};
}

